<?php

namespace Admin\Controller;
use Think\Controller;
use Admin\Model\LoginModel;

class LoginController extends CommonController {

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->User = D('User');//或者 new UserModel();
    }

    /**
     * 登录默认方法
     * @author mmy
     */
    public function index()
    {
        $res = $this->checkLogin();
        if($res)
        {
            $IndexController = A('Index');
            $IndexController->index();
        }
        $this->display('login/index');
    }

    public function test()
    {
        //echo 'hello';
        $res = $this->User->getUserExist('毛毛','123456');
        var_dump($res);
        exit;
    }

    /**
     * 登录方法
     * @author mmy
     */
    public function login()
    {
        $postArray = I('post.');
        if(count($postArray>1))
        {
            $username = $postArray['userName'];
            $userPasswd = $postArray['userPasswd'];
            $validatecode = $postArray['validatecode'];
            if(empty($username))
            {
                $this->showMessage('请输入用户名！');
            }
            if(empty($userPasswd))
            {
                $this->showMessage('请输入密码！');
            }
            if(empty($validatecode))
            {
                $this->showMessage('请输入验证码！');
            }
            //验证验证码
            $result = $this->check_verify($validatecode);
            if($result)
            {
                $res =  $this->User->getUserExist($username,$userPasswd);
                if(count($res)>0)
                {
                    //设置SESSION
                    $this->setSession($res);
                    redirect('admin.php?c=index&a=index');
                }
                else
                {
                    $this->showMessage('用户名或密码不对！');
                }
            }
            else
            {
                $this->showMessage('请输入正确的验证码！');
            }
        }
    }




}